package com.example.demo.controller;

import com.example.demo.config.DataSourceContextHolder;
import com.example.demo.user.User;
import com.example.demo.user.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class TestController {
    @Autowired
    private UserRepository userRepository;

    //http://localhost:8090/test?type=main
    //http://localhost:8090/test?type=child
    @GetMapping("/test")
    public String getAllUsers(@RequestParam(name = "type") String type) {
        DataSourceContextHolder.setDataSourceType(type);
        User user = userRepository.findById(1).orElse(null);
        if (user == null) {
            log.error("is null, please add user");
            return "is null, please add user";
        }
        return user.getUserName();
    }
}